package com.sxp.controller;

import com.sxp.dao.FoodSelectionDaoImpl;
import com.sxp.dao.RoleEnum;
import com.sxp.model.FoodSelection;
import com.sxp.service.FoodSelectionService;
import com.sxp.service.UserListService;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DailyFoodController extends HttpServlet{
    private String LOGIN = "/index.jsp";
    private String LIST_FOOD = "/list_food_selection.jsp";
    private String CUR_SEL = "/current_date_sel.jsp";
    private FoodSelectionService fdsdaoForDate;
    private UserListService userList;
    private  FoodSelectionService foodService;
    private FoodSelectionDaoImpl foodSDao;

    public DailyFoodController() {
        fdsdaoForDate = new FoodSelectionService();
        userList=new UserListService();
        foodSDao=new FoodSelectionDaoImpl();
        foodService=new FoodSelectionService();
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("toDay")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                request.setAttribute("curDate", fdsdaoForDate.curFoodSelDao());
                request.setAttribute("mealL", fdsdaoForDate.getAllMealList());
                request.setAttribute("riceL", fdsdaoForDate.getAllRiceList());
                request.setAttribute("curr",fdsdaoForDate.getCurntDate());
                forward = CUR_SEL;
            }else{
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("update")){
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                forward = LIST_FOOD;
            }else{
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("date")){
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                String date = request.getParameter("SDate");
                try {
                    request.setAttribute("chkDate",fdsdaoForDate.chkDate(date));
                    request.setAttribute("chkDateString",date);
                    request.setAttribute("dateS", fdsdaoForDate.callDaoForDate(date));
                    request.setAttribute("mealL", fdsdaoForDate.getAllMealList());
                    request.setAttribute("riceL", fdsdaoForDate.getAllRiceList());
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                forward = LIST_FOOD;
            }else{
                forward = LOGIN;
            }
        }else {
            forward = LOGIN;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("toDay")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                request.setAttribute("curDate", fdsdaoForDate.curFoodSelDao());
                request.setAttribute("mealL", fdsdaoForDate.getAllMealList());
                request.setAttribute("riceL", fdsdaoForDate.getAllRiceList());
                request.setAttribute("curr",fdsdaoForDate.getCurntDate());
                forward = CUR_SEL;
            }
            else {
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("update")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                FoodSelection foodSel = new FoodSelection();
                foodSel.setUserName(String.valueOf(request.getParameter("user")));
                String editDate = request.getParameter("selDate");
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date editD = sd.parse(editDate);
                    foodSel.setDate(editD);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int riceId = Integer.parseInt(request.getParameter("riceType"));
                foodSel.setRiceId(riceId);
                int i = Integer.parseInt(request.getParameter("ithVal"));
                foodSel.setMealId(Integer.parseInt(request.getParameter("meal" + i)));
                foodService.updateSel(foodSel);
                forward = "DailyFoodController?action=save";
            }else {
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("updateCur")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                FoodSelection foodSel = new FoodSelection();
                foodSel.setUserName(String.valueOf(request.getParameter("user")));
                String editDate = request.getParameter("selDate");
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date editD = sd.parse(editDate);
                    foodSel.setDate(editD);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int riceId = Integer.parseInt(request.getParameter("riceType"));
                foodSel.setRiceId(riceId);
                int i = Integer.parseInt(request.getParameter("ithVal"));
                foodSel.setMealId(Integer.parseInt(request.getParameter("meal" + i)));
                foodService.updateSel(foodSel);
                forward = "DailyFoodController?action=saveCur";
            }else{
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("save")){
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                try {
                    String editDate = request.getParameter("selDate");
                    request.setAttribute("chkDate",foodService.chkDate(editDate));
                    request.setAttribute("chkDateString",editDate);
                    request.setAttribute("dateS", foodService.callDaoForDate(editDate));
                    request.setAttribute("mealL", foodService.getAllMealList());
                    request.setAttribute("riceL", foodService.getAllRiceList());

                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                forward = LIST_FOOD;
            }else {
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("saveCur")){
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                String editDate = request.getParameter("selDate");
                request.setAttribute("curDate", foodService.curFoodSelDao());
                request.setAttribute("mealL", foodService.getAllMealList());
                request.setAttribute("riceL", foodService.getAllRiceList());
                request.setAttribute("curr",fdsdaoForDate.getCurntDate());
                forward = CUR_SEL;
            }else {
                forward = LOGIN;
            }
        }else {
            forward = LOGIN;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }
}
